<template>
	<div class="page">
		<image class="drawBanner" :src="drawSrc"></image>
		<div class="buttonWrap">
			<u-button type="success" shape="circle" open-type="getPhoneNumber" @getphonenumber="weChatLogin">首次授权登录，请绑定手机号
			</u-button>
	<!-- 		<p class="accouintlogin" v-if="inviter_id == ''" >或</p>
			<p class="accouintlogin" v-if="inviter_id == ''" @click="$u.route('/pagesB/login/login')">使用账号密码登录</p> -->
		</div>
		<u-toast ref="uToast" />
	</div>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {},
				drawSrc: '',
				wecode: '',
				inviter_id: '',
				setshareUserId: ''
			}
		},
		onLoad(option) {
			this.userinfo = option
			if(this.userinfo.inviter){
				this.inviter_id = this.userinfo.inviter
			}
			this.setshareUserId = this.$store.state.shareUserId;
			console.log(this.inviter_id)
			this.drawSrc = this.STATICURL + 'images/wechatLogin@2x.png'
			// 1、千万记住在getphone之后不能login，否则session_key就会失效
			// 在onload直接登录获取code,拿着这个code去后台获取session_key
			uni.login({
				provider: "weixin",
				success: loginRes => {
					this.wecode = loginRes.code
					this.getOpenID();
				},
				fail: (err) => {
					this.$toast(JSON.stringify(err));
				},
			});
		},
		methods: {
			// 微信小程序登录   code换区 小程序openid 和 session_key
			getOpenID() {
				return new Promise(resolve => {
					this.$http.getWechatMiniProgramOpenid({
						code: this.wecode
					}).then(res => {
						uni.setStorageSync("xcxOpenid",res.data.openid);
						uni.setStorageSync("xcxSessionKey",res.data.session_key);
						resolve(res.data)
					});
				})
			},
			
			weComfirmChatLogin(e){
				var that = this
				uni.showModal({
					title: '提示',
					content: '授权登录后才能正常使用小程序功能？',
					success: function(res) {
						if (res.confirm) {
							/**
							 * @file: my.vue
							 * @function  weChatLogin
							 * @filepath pages/tabBar/my.vue
							 * @param {type} phone - 用户手机号
							 * @description: 微信小程序登录串行事件
							 * 第一步：获取微信绑定的手机号
							 * 第二步：生成code向后端换取openid
							 * 第三步：获取用户信息以及分销商信息
							 * 第四步:  执行登录-上传以上相关信息参数
							 * @author: jayson
							 * @date: 18:05
							 */
							uni.getUserProfile({
								desc:'获取用户基础信息',
								success: (infoRes) => {
									console.log("bbbbbbbbbbbbbbbbbbbbbbbbbbbb");
									console.log(infoRes)
									that.userinfo= infoRes.userInfo
									console.log(that.userinfo)
									that.weChatLogin(e)
								},
								fail:(res)=>{
									console.log(res);
									console.log("eeeeeeeeeeeeeeeeeeeeeeeeeeeee");
									uni.showToast({
									  icon:"none",
									  title:'用户拒绝获取'
									})
								}
							})
							
						}
					}
				})
			},

			async weChatLogin(e) {
				const userphone = await this.getPhoneNumber(e, uni.getStorageSync('xcxSessionKey'))
				this.$http.getSmsLogin({
					need_sms_code : 0,
					oauth: 'weixin_mp',
					openid: uni.getStorageSync('xcxOpenid'),
					unionid: '',
					nick_name: this.userinfo.nick_name,
					uniongender: this.userinfo.uniongender,
					iconurl: this.userinfo.iconurl,
					phone: userphone.phone,
					inviter_id: this.userinfo.inviter,
					setshareUserId: this.setshareUserId,
				}).then(dengluRes => {
					if (dengluRes.status == 200) {
						this.$store.commit('loginStatus', true)
						uni.setStorageSync('token', dengluRes.data.token);
						uni.setStorageSync('identity', dengluRes.data.identity);
						uni.setStorageSync('role', dengluRes.data.role);
						uni.setStorageSync('role_id', dengluRes.data.shop_id);
						
						uni.closeSocket({
							success() {
								console.log('socket关闭成功');
							},
							fail(res) {
								console.log(res);
							}
						})
						
						setTimeout(() => {
							this.im();
						}, 500);
						this.$toast('登录成功');
						uni.navigateBack({
						    delta: 1 // 默认值是1，表示返回的页面层数
						});
						
						// this.$refs.uToast.show({
						// 	title: '登录成功',
						// 	type: 'success',
						// 	duration: 500,
						// 	isTab: 'true',
						// 	url: '/pages/tabBar/my'
						// })
				
					}
				})

			},
			//获取微信手机号
			getPhoneNumber(e = {}, key = "") {

				const params = {
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					sessionKey: key,
				};
				return new Promise((resolve, reject) => {
					this.$http.getPhoneNumber(params).then(res => {
						if (res.status === 200) {
							resolve(res.data);
						} else {
							reject(res.mess)
						}
					});
				});
			},

			asyncUerInfo(openid = '', phone, unionid = '') {
				/**
				 * @file: miniProgramLogin.js
				 * @function  asyncUerInfo
				 * @filepath utils/miniProgramLogin.js
				 * @param {Object} res - 用户信息
				 * @return {String} openid - 小程序用户openid
				 * @description:
				 * @author: jayson
				 * @date: 18:34
				 */

				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {}

	.page {
		background-color: #fff;
		height: 100vh;
		padding: 60upx 30upx;
		text-align: center;
	}

	.drawBanner {
		width: 390upx;
		height: 390upx;

	}

	.buttonWrap {
		margin: 40upx;

		.accouintlogin {
			color: #444;
			margin-top: 20upx
		}
	}
</style>
